#pragma once
#include<assert.h>

namespace buluo
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T data;

		list_node(const T& s = T())
			:_next(nullptr)
			, _prev(nullptr)
			, data(s)
		{}
	};

	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T, Ref, Ptr> self;
		node* _node;
		list_iterator(node* n)
			:_node(n)
		{}
		Ref operator*()
		{
			return _node->data;
		}
		Ptr operator->()
		{
			return &_node->data;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
	};

	template<class T>
	class list
	{
		
	public:
		typedef list_node<T> node;
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		list()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}

		list<T>& operator=(list<T> n)
		{
			swap(n);
			return *this;
		}
		list(const list<T>& n)
			:_head(new node)
		{
			_head->_next = _head;
			_head->_prev = _head;
			list<T> tmp(n.begin(), n.end());
			swap(tmp);
		}
		template <class InputIterator>
		list(InputIterator first, InputIterator last)
			:_head(new node)
		{
			_head->_next = _head;
			_head->_prev = _head;
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}

		void push_back(const T& s)
		{
			node* newnode = new node(s);
			node* tail = _head->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}
		void push_front(const T& s)
		{
			insert(begin(), s);
		}
		void pop_back()
		{
			erase(iterator(_head->_prev));
		}
		void pop_front()
		{
			erase(begin());
		}

		void insert(iterator pos, const T& s)
		{
			node* newnode = new node(s);
			node* prev = pos._node->_prev;
			newnode->_next = pos._node;
			newnode->_prev = prev;
			prev->_next = newnode;
			pos._node->_prev = newnode;

		}
		void erase(iterator pos)
		{
			assert(pos != end());
			node* next = pos._node->_next;
			node* prev = pos._node->_prev;
			prev->_next = next;
			next->_prev = prev;
			delete pos._node;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

	private:
		node* _head;
	};

	void test_list()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
		list<int>::iterator pos = lt.begin();
		lt.insert(pos, 10);
		lt.push_front(100);
		lt.pop_front();
		lt.pop_back();
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
		list<int> n(lt);
		for (auto e : n)
		{
			cout << e << " ";
		}
		cout << endl;
		list<int> n1 = n;
		for (auto e : n1)
		{
			cout << e << " ";
		}
	}
}
